package com.reremouse.rerebbs.dao.impl;

import com.reremouse.rerebbs.dao.IBbsTalkDAO;
import com.reremouse.rerebbs.model.BbsTalk;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

public class BbsTalkDAOImpl
        implements IBbsTalkDAO, Serializable {
    @PersistenceContext
    private EntityManager entityManager;
    public static final String CONTENT = "content";
    public static final String THREAD = "thread";
    public static final String GEN_BY = "genBy";
    public static final String IF_PUB = "ifPub";

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void save(BbsTalk entity) {
        try {
            this.entityManager.persist(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void delete(BbsTalk entity) {
        try {
            entity = (BbsTalk) this.entityManager.getReference(BbsTalk.class, entity.getId());

            this.entityManager.remove(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public BbsTalk update(BbsTalk entity) {
        try {
            return (BbsTalk) this.entityManager.merge(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public BbsTalk findById(Long id) {
        try {
            return (BbsTalk) this.entityManager.find(BbsTalk.class, id);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<BbsTalk> findByProperty(String propertyName, Object value, int... rowStartIdxAndCount) {
        try {
            String queryString = "select model from BbsTalk model where model." + propertyName + "= :propertyValue order by model.genTime";

            Query query = this.entityManager.createQuery(queryString);
            query.setParameter("propertyValue", value);
            if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }
                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);
                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }
            return query.getResultList();
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<BbsTalk> findByContent(Object content, int... rowStartIdxAndCount) {
        return findByProperty("content", content, rowStartIdxAndCount);
    }

    public List<BbsTalk> findByThread(Object thread, int... rowStartIdxAndCount) {
        return findByProperty("thread", thread, rowStartIdxAndCount);
    }

    public List<BbsTalk> findByGenBy(Object genBy, int... rowStartIdxAndCount) {
        return findByProperty("genBy", genBy, rowStartIdxAndCount);
    }

    public List<BbsTalk> findByIfPub(Object ifPub, int... rowStartIdxAndCount) {
        return findByProperty("ifPub", ifPub, rowStartIdxAndCount);
    }

    public List<BbsTalk> findAll(int... rowStartIdxAndCount) {
        try {
            String queryString = "select model from BbsTalk model order by model.genTime";
            Query query = this.entityManager.createQuery("select model from BbsTalk model order by model.genTime");
            if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }
                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);
                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }
            return query.getResultList();
        } catch (RuntimeException re) {
            throw re;
        }
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.BbsTalkDAOImpl

 * JD-Core Version:    0.7.0.1

 */